package com.shanghaionstar.user.service;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onstar.cn.api.UsersService;
import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.more.entity.VehicleImgSave;
import com.onstar.cn.more.entity.VehicleImgs;
import com.onstar.cn.more.response.VehicleImgRes;
import com.shanghaionstar.enumtype.DealerBrandType;
import com.shanghaionstar.enumtype.OSTypeEnum;
import com.shanghaionstar.user.FeedBackResult;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import java.lang.reflect.Type;
import java.util.List;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Mengzhou Gu on 2016/8/30.
 */
public class ChooseVehicleImgService extends BaseService<List<VehicleImgRes>> {
    private Context context;

    public ChooseVehicleImgService(Context context) {
        this.context = context;
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    @Override
    public RequestEntity getRequestEntity() {
        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        String ipdID = ApplicationUtil.getInstance().getIdpID();
        String accountID = ApplicationUtil.getInstance().getAccountId();
        Vehicle v = ApplicationUtil.getInstance().getCurrentVehicle();
        String model = v.getModelDesc();
        String vin = ApplicationUtil.getInstance().getVin();
        service.getVehicleImgs(requestEntity, ipdID, accountID, vin);
        VehicleImgs vImgs = new VehicleImgs();
        String brand = DeviceUtil.isChineseLanguage() ? DealerBrandType.getBriefNameByDesc(model)
                : DealerBrandType.getDealerBrandTypeByDesc(model).value(); // 经销商品牌
        vImgs.setBrand(brand == null ? "" : brand);
        vImgs.setOsType(OSTypeEnum.ANDROID_SMALL.value());
        requestEntity.setBody(GsonHelper.toJson(vImgs));
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "getRequestEntity", requestEntity);
        return requestEntity;
    }

    @Override
    public List<VehicleImgRes> jsonToBean(String json) {
      /*  return (List<VehicleImgRes>)JsonParseUtil.JsonToCollection(json);*/
        Type type = new TypeToken<List<VehicleImgRes>>() {
        }.getType();
        List<VehicleImgRes> mImgList = new Gson().fromJson(json, type);
        return mImgList;
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public Observable<FeedBackResult> getObserval(final String imageID) {
        return Observable.defer(new Func0<Observable<FeedBackResult>>() {
            @Override
            public Observable<FeedBackResult> call() {
                try {
                    UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                    String ipdID = ApplicationUtil.getInstance().getIdpID();
                    String accountID = ApplicationUtil.getInstance().getAccountId();
                    String vin = ApplicationUtil.getInstance().getVin();
                    service.saveVehicleImg(requestEntity, ipdID, accountID, vin);
                    VehicleImgSave vImg = new VehicleImgSave();
                    vImg.setImageID(imageID);
                    vImg.setVin(vin);
                    requestEntity.setBody(GsonHelper.toJson(vImg));
                    requestEntity = HttpInjectUtils.injectHttp(ChooseVehicleImgService.this.getClass(), "getObserval", requestEntity, String.class);
                    return Observable.just((FeedBackResult) GsonHelper.fromJson(BusinessUtil.getResponse(getDatas(requestEntity), context),FeedBackResult.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }
}